package com.atlansheng.rocketmqdemoboot.demos.producer;

import com.alibaba.fastjson.JSON;
import com.atlansheng.rocketmqdemoboot.demos.Dto.MsgInfo;
import com.atlansheng.rocketmqdemoboot.demos.conf.MyRocketMqTemplete;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.rocketmq.spring.support.RocketMQHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

/**
 * @ClassName: ProducerSend2
 * @Description:
 * @Date 2024/05/09 09:29
 * @Author songshaoqiu
 **/
@Component
@Slf4j
public class ProducerSend2 {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    private final String topic ="delay-topic-02";
    /**
     * 延迟消息
     * @param id
     * @param name
     */
    public void sendDelay(Long id, String name){
        MsgInfo msgInfo = new MsgInfo();
        msgInfo.setId(id);
        msgInfo.setName(name);
        msgInfo.setAge(66);

        Message<String> message = MessageBuilder.withPayload(JSON.toJSONString(msgInfo))
                .setHeader("KEYS", msgInfo.getId())
                .setHeader(RocketMQHeaders.TAGS,"hello")
                .build();
        SendResult sendResult = rocketMQTemplate.syncSendDelayTimeMills(topic, message, 5 * 1000);
        log.info("模拟发送5秒后消费消息{}",sendResult);
    }

}
